package cn.xz.zysjz.utils;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.ImageView;

import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSONArray;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.xz.zysjz.R;
import cn.xz.zysjz.base.BaseApplication;
import cn.xz.zysjz.bean.CommonLib;

/**
 * 获取APP的一些基本信息工具类-比如APP版本号等*/
public class AppHelper {

    //获取APP的图片
    public static Drawable getDrawable(int id){
        return ContextCompat.getDrawable(BaseApplication.getContext(),id);
    }

    //给图片设置内容
    public static void setPic(ImageView iv,String url){
        Glide.with(iv)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                .into(iv);
    }

    public static void setLocalPic(ImageView iv,String url){
        if (url.contains("test")){
            Glide.with(BaseApplication.getContext())
                    .load("http://hualao.icu:10001/" + url)
                    //.apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
                    .into(iv);
        }else {
            Glide.with(iv)
                    .load(AppHelper.getDrawable(BaseApplication.getContext().getResources().getIdentifier(url, "mipmap" , BaseApplication.getContext().getPackageName())))
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                    .into(iv);
        }
    }

    public static void setPic(ImageView iv,Drawable url){
        Glide.with(iv)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                .into(iv);
    }

    public static void setPic(ImageView iv, Uri url){
        Glide.with(iv)
                .load(url)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(10)))
                .into(iv);
    }

    //校验手机号
    public static Boolean isMobileNO(String mobiles){
        Boolean flag = false;
        try {
            Pattern p = Pattern.compile("^1[3-9]\\d{9}$");
            Matcher m  = p.matcher(mobiles);
            flag = m.matches();
        } catch ( Exception e) {
            // LOG.error("验证手机号码错误", e);
            flag = false;
        }
        return flag;
    }

    //跳转界面
    public static void toJump(Class cls){
        Intent intent = new Intent(BaseApplication.getContext(), cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        BaseApplication.getContext().startActivity(intent);
    }

    public static void toJump(Class cls,String s){
        Intent intent = new Intent(BaseApplication.getContext(), cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(CommonLib.JUMP_TYPE,s);
        BaseApplication.getContext().startActivity(intent);
    }

    public static void toJump(Class cls, Serializable s){
        Intent intent = new Intent(BaseApplication.getContext(), cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra(CommonLib.JUMP_TYPE,s);
        BaseApplication.getContext().startActivity(intent);
    }

    //解析对应的数据
    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }



    //获取当前版本号
    public static String getAppVersionName(){
        String mS = "1.0.0";
        try {
            PackageManager packageManager = BaseApplication.getContext().getPackageManager();
            PackageInfo packageInfo =
                    packageManager.getPackageInfo(BaseApplication.getContext().getPackageName(), 0);
            mS = packageInfo.versionName;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mS;
    }

    //获取时间
    public static String getTimer(){
        String s = "";
        Calendar calendar = Calendar.getInstance();
        //年
        int year = calendar.get(Calendar.YEAR);
        //月
        int month = calendar.get(Calendar.MONTH) + 1;
        //日
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        //小时
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        //分钟
        int minute = calendar.get(Calendar.MINUTE);
        s= year + "-" + month + "-" + day + " " + hour + ":" + minute;
        return s;
    }
}
